import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;
import Carlos from "./carlos.js";
import { CarlosI } from "./carlos-i";

@ccclass('CarlosEngine')
export class CarlosEngine {
    private _carlos: any = null;
    private _engine: CarlosI.Engine = null;

    private static _instance: CarlosEngine = null;
    public static get instance() {
        if (!this._instance) {
            this._instance = new CarlosEngine();
        }
        return this._instance;
    }

    public async init() {
        this._carlos = await Carlos();
        this._engine = new this._carlos.Engine();
        
    }
    public addNode(uuid: string): CarlosI.Node {
        if (!this._engine) {
            console.log(
                "%c🚀Carlos Engine: %c引擎未正常初始化", 
                "color: red; font-weight: bold;", 
                "color: inherit;"
              );
            return null;
        }
        return this._engine.addNode(uuid);
    }
    public update(dt: number): void {
        if (!this._engine) return;
        this._engine.update(dt);
    }


    //物理系统管理
    /**
     * 开启物理系统
     */
    public openPhysics(): void {
        if (!this._engine) {
            console.log(
                "%c🚀Carlos Engine: %c引擎未正常初始化", 
                "color: red; font-weight: bold;", 
                "color: inherit;"
              );
            return;
        };
        this._engine.openPhysics();
    }
    /**
     * 关闭物理系统
     */
    public closePhysics(): void {
        if (!this._engine) {
            console.log(
                "%c🚀Carlos Engine: %c引擎未正常初始化", 
                "color: red; font-weight: bold;", 
                "color: inherit;"
              );
            return;
        };
        this._engine.destroyPhysics();
    }
}

